package com.jy.appmarket.utils;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;

public final class SysUtils {

	private SysUtils() {
	}

	private static void safeClose(Closeable c) {
		if (c == null) {
			return;
		}
		try {
			c.close();
		} catch (IOException e) {
			// do nothing
		}
	}

	public static byte[] assetFile2Bytes(Context context, String path) {
		InputStream in = null;
		try {
			in = context.getAssets().open(path);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[10240];
			int readed = 0;
			while ((readed = in.read(buffer)) > 0) {
				baos.write(buffer, 0, readed);
			}
			return baos.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			safeClose(in);
		}
		return null;
	}
}